package com.gable.exhibition.controller;

import com.alibaba.fastjson.JSON;
import com.gable.common.util.CacheData;
import com.gable.common.util.ReturnResultUtil;
import com.gable.common.util.StringUtil;
import com.gable.common.util.UUIDGenerator;
import com.gable.exhibition.dao.MaintainerDao;
import com.gable.exhibition.dao.UserDao;
import com.gable.exhibition.model.*;
import com.gable.exhibition.service.MaintainerService;
import com.gable.exhibition.service.UpkeepService;
import com.gable.exhibition.service.UserService;
import com.gable.exhibition.util.MessageUtil;
import com.gable.exhibition.view.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by 乔春峰 on 2018/1/4.
 * 保养controller
 */
@Controller
@RequestMapping("/upkeep")
public class UpkeepController {

    Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    UpkeepService upkeepService;

    @Autowired
    UserService userService;

    @Autowired
    MaintainerService maintainerService;

    /**
     * 跳转页面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/page")
    public String page(HttpServletRequest request, HttpServletResponse response){
        return "upkeep/upkeep-page";
    }

    /**
     * 跳转详情
     * @param request
     * @param response
     * @param upkeepId
     * @return
     */
    @RequestMapping(value="/info")
    public String info(HttpServletRequest request, HttpServletResponse response, @RequestParam(required = true) Long upkeepId){
        request.setAttribute("upkeepId",upkeepId);
        return "upkeep/upkeep-info";
    }

    /**
     * 保养列表
     * @param request
     * @param response
     * @param upkeepView
     * @return
     */
    @RequestMapping(value="/queryUpkeepList",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryUpkeepList(HttpServletRequest request, HttpServletResponse response, UpkeepView upkeepView){
        try {
            Long userId = null;
            if (!StringUtil.isEmpty(request.getParameter("userId"))) {
                userId = Long.parseLong(request.getParameter("userId"));
            }
            User user = CacheData.getSessionUser(userId, 1);
            if (user.getType().longValue() == 1101) {
                upkeepView.setHospitalId(user.getHospitalId());
                upkeepView.setRole(1);
            } else if(user.getTag().longValue() == 1004 || user.getTag().longValue() == 1006 || user.getTag().longValue() ==1009) {
                upkeepView.setRole(3);
                upkeepView.setEngineerId(user.getUserId());
            }else if(user.getType().longValue() == 1102 || user.getType().longValue() == 1103 || user.getType().longValue() ==1105){
                upkeepView.setRole(2);
                upkeepView.setServiceType(user.getType());
                if(user.getType().longValue() == 1102){
                    upkeepView.setServiceId(user.getManufacturerId());
                }else if(user.getType().longValue() == 1103){
                    upkeepView.setServiceId(user.getMaintainerId());
                }else if(user.getType().longValue() == 1105){
                    upkeepView.setServiceId(user.getSupplierId());
                }
            }else if(user.getType().longValue() == 1104){
                upkeepView.setRole(4);
            }
            ReturnResultUtil rru = upkeepService.queryUpkeepList(upkeepView);
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnResultUtil(500, MessageUtil.QUERYEXCEPTION);
    }

    /**
     * 新增保养信息
     * @param request
     * @param response
     * @param upkeepView
     * @return
     */
    @RequestMapping(value="/addUpkeep",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil addUpkeep(HttpServletRequest request, HttpServletResponse response, UpkeepView upkeepView){
        try{

            User user = CacheData.getSessionUser(upkeepView.getUserId(),1);
            upkeepView.setUserId(user.getUserId());
            if(upkeepView.getDeviceId() == null){
                return new ReturnResultUtil(500,MessageUtil.DEVICEIDISNULL);
            }
            if(upkeepView.getHospitalId() == null){
                upkeepView.setHospitalId(user.getHospitalId());
            }
            upkeepView.setCreatePerson(user.getUserId());
            int hospital_user = 0 ;
            if(!StringUtil.isEmpty(request.getParameter("hospital_user"))){
                hospital_user = Integer.parseInt(request.getParameter("hospital_user"));
            }
            if(hospital_user == 1){
                if(StringUtil.isEmpty(upkeepView.getEngineerName()) || StringUtil.isEmpty(upkeepView.getMobile())){
                    return new ReturnResultUtil(500,MessageUtil.ENGINEERINFOISNULL);
                }
            }
            ReturnResultUtil rru = upkeepService.addUpkeep(upkeepView,hospital_user);
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnResultUtil(500,MessageUtil.APPLYUPKEEPEXCEPTION);
    }


    /**
     * 查询保养详情
     * @param request
     * @param upkeepView
     * @return
     */
    @RequestMapping(value="/queryUpkeepInfo",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryUpkeepInfo(HttpServletRequest request, UpkeepView upkeepView){
        try{
            Long upkeepId = upkeepView.getUpkeepId();
            if(upkeepId == null){
                return new ReturnResultUtil(500,MessageUtil.UPKEEPIDISNULL);
            }

            UserView sessionUser = CacheData.getSessionUser(upkeepView.getUserId(), 1);
            if(sessionUser == null)
                sessionUser = CacheData.getSessionUser(null,2);
            if(sessionUser == null)
                return new ReturnResultUtil(600,MessageUtil.SESSIONNOTEXITIS);
            upkeepView.setUserId(sessionUser.getUserId());

            ReturnResultUtil rru = upkeepService.queryUpkeepInfo(upkeepView);
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
    }

    /**
     * 平台分配服务商初始化数据
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/assignServiceInit",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil assignServiceInit(HttpServletRequest request, HttpServletResponse response){
        try{
            if(StringUtil.isEmpty(request.getParameter("upkeepId"))){
                return  new ReturnResultUtil(500,MessageUtil.UPKEEPIDISNULL);
            }

            Long upkeeepId = Long.parseLong(request.getParameter("upkeepId"));
            ReturnResultUtil rru = upkeepService.assignServiceInit(upkeeepId);
            logger.info(JSON.toJSONString(rru));
            return  rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
    }


/*    @RequestMapping(value="/queryEngineerPhoneByName",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryEngineerPhoneByName(HttpServletRequest request, HttpServletResponse response){

        try {
            if (StringUtil.isEmpty(request.getParameter("maintainerId"))) {
                return new ReturnResultUtil(500);
            }
            if (StringUtil.isEmpty(request.getParameter("engineerName"))) {
                return new ReturnResultUtil(500);
            }
            Long maintainerId = Long.parseLong(request.getParameter("maintainerId"));
            String engineerName = request.getParameter("engineerName");
            ReturnResultUtil rru = upkeepService.queryEngineerPhoneByName(maintainerId, engineerName);
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
    }*/

    /**
     * 平台分配服务商
     * @param request
     * @param response
     * @param upkeepView
     * @return
     */
    @RequestMapping(value="/saveAssign",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil saveAssign(HttpServletRequest request, HttpServletResponse response,UpkeepView upkeepView){
        try{
            User loginUser = CacheData.getSessionUser(null,1);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            if(StringUtil.isEmpty(request.getParameter("upkeepId"))){
                return  new ReturnResultUtil(500,MessageUtil.UPKEEPIDISNULL);
            }

            if(StringUtil.isEmpty(request.getParameter("is_dispatch"))){
                return  new ReturnResultUtil(500,MessageUtil.ISDISATCHISNULL);
            }

            if(upkeepView.getServiceType() == null){
                return new ReturnResultUtil(500,MessageUtil.SERVICETYPE);
            }

            if(upkeepView.getServiceType() == 1103 && (upkeepView.getServiceId() == null)){
                Maintainer maintainer = new Maintainer();
                maintainer.setMaintainerName(upkeepView.getServiceName());
                maintainer.setMaintainerPhone(upkeepView.getServicePhone());
                maintainer.setMaintainerContacts(upkeepView.getServiceContacts());
                Long serviceId = maintainerService.checkMaintainer(maintainer);
                upkeepView.setServiceId(serviceId);
            }
            int isDispatch = Integer.parseInt(request.getParameter("is_dispatch"));
            if(upkeepView.getServiceId() != null && upkeepView.getEngineerId() != null){
                upkeepView.setStatus(1806L);
            }else{
                upkeepView.setStatus(1801L);
            }

            upkeepView.setHandleTime(df.format(new Date()));
            upkeepView.setTerraceHandlePerson(loginUser.getUserId());
            upkeepView.setUpdatePerson(loginUser.getUserId());
            upkeepView.setUpdateTime(df.format(new Date()));
            if(isDispatch == 1 && upkeepView.getEngineerId() == null){
                upkeepView.setStatus(1806L);
                ReturnResultUtil rru = userService.queryUserByEngineerPhone(upkeepView.getEngineerPhone(),upkeepView.getEngineerName(),upkeepView.getServiceId(),upkeepView.getServiceType());
                if(rru.getRet() == 500){
                    return rru;
                }
                User user = (User) rru.getData();
                upkeepView.setEngineerId(user.getUserId());
            }
            ReturnResultUtil rru = upkeepService.saveAssign(upkeepView,loginUser);
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnResultUtil(500,MessageUtil.SAVEEXCEPTION);
    }


    /**
     * 保存确认保养信息
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/saveComple",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil saveComple(HttpServletRequest request, HttpServletResponse response){
        try{
            Long userId = null;
            if(!StringUtil.isEmpty(request.getParameter("userId"))){
                userId = Long.parseLong(request.getParameter("userId"));
            }
            User loginUser = CacheData.getSessionUser(userId,1);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(StringUtil.isEmpty(request.getParameter("upkeepId"))){
                return new ReturnResultUtil(500,MessageUtil.UPKEEPIDISNULL);
            }
            if(StringUtil.isEmpty(request.getParameter("status"))){
                return new ReturnResultUtil(500,MessageUtil.STATUSISNULL);
            }
            Long upkeepId = Long.parseLong(request.getParameter("upkeepId"));
            String comment = request.getParameter("comment");
            Long status = Long.parseLong(request.getParameter("status"));
            UpkeepView upkeep = new UpkeepView();
            upkeep.setUpdatePerson(loginUser.getUserId());
            upkeep.setUpdateTime(df.format(new Date()));
            upkeep.setUpkeepId(upkeepId);
            upkeep.setComment(comment);
            upkeep.setStatus(status);
            upkeep.setConfirmPerson(loginUser.getUserId());
            upkeep.setConfirmTime(df.format(new Date()));
            ReturnResultUtil rru = upkeepService.saveComple(upkeep,loginUser);
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnResultUtil(500,MessageUtil.CONFIRMEXCEPTION);
    }

    /**
     * 保养数据补全信息
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/saveUpkeepData",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil saveUpkeepData(HttpServletRequest request, HttpServletResponse response){
        try{
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Long userId = null;
            if(!StringUtil.isEmpty(request.getParameter("userId"))){
                userId =Long.parseLong(request.getParameter("userId"));
            }
            User user = CacheData.getSessionUser(userId,1);
            if(StringUtil.isEmpty(request.getParameter("upkeepId"))){
                return new ReturnResultUtil(500,MessageUtil.UPKEEPIDISNULL);
            }

            Long upkeepId = Long.parseLong(request.getParameter("upkeepId"));
            Integer travelTime =null;
            if(!StringUtil.isEmpty(request.getParameter("travelTime"))){
                travelTime = Integer.parseInt(request.getParameter("travelTime"));
            }
            String upkeepEndTime = null;
            if(!StringUtil.isEmpty(request.getParameter("upkeepEndTime"))){
                upkeepEndTime = request.getParameter("upkeepEndTime");
            }
            Long endDeviceStatus = null;
            if(!StringUtil.isEmpty(request.getParameter("endDeviceStatus"))){
                endDeviceStatus = Long.parseLong(request.getParameter("endDeviceStatus"));
            }
            String upkeepContent = null;
            if(!StringUtil.isEmpty(request.getParameter("upkeepContent"))){
                upkeepContent = request.getParameter("upkeepContent");
            }
            List<FileView> fileList = new ArrayList<>();
            if(!StringUtil.isEmpty(request.getParameter("fileList"))){
                String fileListStr = request.getParameter("fileList");
                fileList = JSON.parseArray(fileListStr,FileView.class);
            }



            UpkeepView upkeepView = new UpkeepView();
            upkeepView.setStatus(1803L);
            upkeepView.setUpkeepId(upkeepId);
            upkeepView.setFileList(fileList);
            upkeepView.setTravelTime(travelTime);
            upkeepView.setEndDeviceStatus(endDeviceStatus);
            upkeepView.setUpkeepContent(upkeepContent);
            upkeepView.setUpkeepEndTime(upkeepEndTime);
            upkeepView.setUpdateTime(df.format(new Date()));
            upkeepView.setUpdatePerson(user.getUserId());
            ReturnResultUtil rru = upkeepService.saveUpkeepData(upkeepView,user);
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnResultUtil(500,MessageUtil.SAVEEXCEPTION);
    }

    /**
     * 查询当前设备是否在保养中
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/queryDeviceIsUpkeep",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceIsUpkeep(HttpServletRequest request,HttpServletResponse response){
        try{
            if(StringUtil.isEmpty(request.getParameter("deviceId"))){
                new ReturnResultUtil(500,MessageUtil.DEVICEIDISNULL);
            }
            Long deviceId = Long.parseLong(request.getParameter("deviceId"));
            return upkeepService.queryDeviceIsUpkeep(deviceId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
    }

    /**
     * 保存审核的维修商信息
     * @param request
     * @param response
     * @param maintainer
     * @param upkeep
     * @return
     */
    @RequestMapping(value="/saveExamineMaintainer",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil saveExamineMaintainer(HttpServletRequest request,HttpServletResponse response,Maintainer maintainer,Upkeep upkeep){
        try{
            Long userId = null;
            if(!StringUtil.isEmpty(request.getParameter("userId"))){
                userId = Long.parseLong(request.getParameter("userId"));
            }
            if(StringUtil.isEmpty(request.getParameter("upkeepId"))){
                return new ReturnResultUtil(500,MessageUtil.UPKEEPIDISNULL);
            }
            Long upkeepId =  Long.parseLong(request.getParameter("upkeepId"));
            User user = CacheData.getSessionUser(userId,1);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            maintainer.setIsDel(0);
            maintainer.setMaintainerId(UUIDGenerator.getUniqueLong());
            maintainer.setCreatePerson(user.getUserId());
            maintainer.setRemark("医院端创建");
            ReturnResultUtil rru = upkeepService.saveExamineMaintainer(maintainer,upkeep);
            return  upkeepService.assignServiceInit(upkeepId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
    }

    /**
     * 指派服务商或者直接到工程师
     * @param request
     * @param response
     * @param appoint
     * @return
     */
    @RequestMapping(value="/assignServiceOrEngineer",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil assignServiceOrEngineer(HttpServletRequest request,HttpServletResponse response,Appoint appoint){
            try{

                Long userId = null;
                if(!StringUtil.isEmpty(request.getParameter("userId"))){
                    userId = Long.parseLong(request.getParameter("userId"));
                }

                User loginUser = CacheData.getSessionUser(userId,1);

                if(appoint.getInstructId() == null){
                    return  new ReturnResultUtil(500,MessageUtil.INSTRUCTIDISNULL);
                }
                if(appoint.getInstructType() == null){
                    return new ReturnResultUtil(500,MessageUtil.INSTRUCTTYPEISNULL);
                }
           /*     if(appoint.getInstructedId() == null){
                    return  new ReturnResultUtil(500,MessageUtil.INSTRUCTEDIDISNULL);
                }*/
                if(appoint.getInstructedType() == null){
                    return  new ReturnResultUtil(500,MessageUtil.INSTRUCTEDTYPEISNULL);
                }
                if(appoint.getKeyId() == null ){
                    return new ReturnResultUtil(500,MessageUtil.UPKEEPIDISNULL);
                }

                if(StringUtil.isEmpty(request.getParameter("is_engineer"))){
                    return new ReturnResultUtil(500,MessageUtil.ISENGINEERISNULL);
                }

                if(appoint.getInstructedType().longValue() == 1103 && appoint.getInstructedId() == null){
                        Maintainer maintainer = new Maintainer();
                        String servicePhone = request.getParameter("servicePhone");
                        String serviceContacts = request.getParameter("serviceContacts");
                        String serviceName = request.getParameter("serviceName");
                        maintainer.setMaintainerPhone(servicePhone);
                        maintainer.setMaintainerContacts(serviceContacts);
                        maintainer.setMaintainerName(serviceName);
                        Long instructedId = maintainerService.checkMaintainer(maintainer);
                        appoint.setInstructedId(instructedId);
                }

                int is_engineer = Integer.parseInt(request.getParameter("is_engineer"));
                if(is_engineer == 1){
                    if(StringUtil.isEmpty(appoint.getEngineerName())){
                        return new ReturnResultUtil (500,MessageUtil.ENGINEERINFOISNULL);
                    }
                    if(StringUtil.isEmpty(appoint.getEngineerPhone())){
                        return new ReturnResultUtil(500,MessageUtil.ENGINEERPHONEISNULL);
                    }

                    ReturnResultUtil rru = userService.queryUserByEngineerPhone(appoint.getEngineerPhone(),appoint.getEngineerName(),appoint.getInstructedId(),appoint.getInstructedType());
                    if(rru.getRet() == 500){
                        return rru;
                    }
                    User user = (User) rru.getData();
                    appoint.setEngineerId(user.getUserId());
                }

                appoint.setCreatePerson(loginUser.getUserId());
                return upkeepService.assignServiceOrEngineer(appoint);
            }catch (Exception e){
                e.printStackTrace();
            }
        return new ReturnResultUtil(500,MessageUtil.SAVEEXCEPTION);
    }

    /**
     * 修改工程师姓名和电话接口
     * @param request
     * @param response
     * @param appoint
     * @return
     */
    @RequestMapping(value="/updateEngineerNameAndEngineerPhone",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil updateEngineerNameAndEngineerPhone(HttpServletRequest request,HttpServletResponse response,Appoint appoint){
        try{
            Long userId = null;
            if(!StringUtil.isEmpty(request.getParameter("userId"))){
                userId = Long.parseLong(request.getParameter("userId"));
            }

            User loginUser = CacheData.getSessionUser(userId,1);

            if(appoint.getAppointId() == null){
                return new ReturnResultUtil(500,MessageUtil.APPOINTIDISNULL);
            }

            if(StringUtil.isEmpty(appoint.getEngineerName())){
                return new ReturnResultUtil (500,MessageUtil.ENGINEERINFOISNULL);
            }
            if(StringUtil.isEmpty(appoint.getEngineerPhone())){
                return new ReturnResultUtil(500,MessageUtil.ENGINEERPHONEISNULL);
            }
            appoint.setCreatePerson(loginUser.getUserId());
            return upkeepService.updateEngineerNameAndEngineerPhone(appoint,loginUser);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnResultUtil(500,MessageUtil.SAVEEXCEPTION);
    }

    @RequestMapping(value="/dxpectedToDoorByEngineer",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil dxpectedToDoorByEngineer(HttpServletRequest request,HttpServletResponse response){
        try{
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Long userId = null;
            if(!StringUtil.isEmpty(request.getParameter("userId"))){
                userId = Long.parseLong(request.getParameter("userId"));
            }

            User user = CacheData.getSessionUser(userId,1);

            if(StringUtil.isEmpty(request.getParameter("upkeepId"))){
                return new ReturnResultUtil(500,MessageUtil.UPKEEPIDISNULL);
            }
            Long upkeepId = Long.parseLong(request.getParameter("upkeepId"));


            String engineerExpectedTime = null;
            if(!StringUtil.isEmpty(request.getParameter("engineerExpectedTime"))){
                engineerExpectedTime = request.getParameter("engineerExpectedTime");
            }

            Upkeep upkeep = new Upkeep();
            upkeep.setUpkeepId(upkeepId);
            upkeep.setEngineerExpectedTime(engineerExpectedTime);
            upkeep.setUpdateTime(df.format(new Date()));
            upkeep.setUpdatePerson(user.getUserId());
            upkeep.setEngineerResponseTime(df.format(new Date()));
            upkeep.setStatus(1802L);
            return upkeepService.dxpectedToDoorByEngineer(upkeep);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnResultUtil(500,MessageUtil.SAVEEXCEPTION);
    }

    @RequestMapping(value="/remindUpkeepList",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil remindUpkeepList(HttpServletRequest request,HttpServletResponse response){
        try{
            Long userId = null;
            if(!StringUtil.isEmpty(request.getParameter("userId"))){
                userId = Long.parseLong(request.getParameter("userId"));
            }
            User user = CacheData.getSessionUser(userId,1);
            Map map = new HashMap();
            if(user.getType().longValue() == 1101){
                map.put("role",1);
                map.put("hospitalId",user.getHospitalId());
            }else if(user.getTag().longValue() == 1004 || user.getTag().longValue() == 1006 || user.getTag().longValue() ==1009) {
                map.put("role",3);
                map.put("engineerId",user.getUserId());
            }else if(user.getType().longValue() == 1102 || user.getType().longValue() == 1103 || user.getType().longValue() ==1105){
                map.put("role",2);
                if(user.getType().longValue() == 1102){
                    map.put("serviceId",user.getManufacturerId());
                }else if(user.getType().longValue() == 1103){
                    map.put("serviceId",user.getMaintainerId());
                }else if(user.getType().longValue() == 1105){
                    map.put("serviceId",user.getSupplierId());
                }
            }
            ReturnResultUtil rru = upkeepService.remindUpkeepList(map);
            logger.debug(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
    }

    @RequestMapping(value="/queryPhoneByEngineerName",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryPhoneByHospital(HttpServletRequest request,HttpServletResponse response){
        try {
            Long userId = null;
            if (!StringUtil.isEmpty(request.getParameter("userId"))) {
                userId = Long.parseLong(request.getParameter("userId"));
            }
            User user = CacheData.getSessionUser(userId, 1);
            if (StringUtil.isEmpty(request.getParameter("keyword"))) {
                return new ReturnResultUtil(200);
            }

            Map map = new HashMap();
            String keyword = request.getParameter("keyword");
            map.put("hospitalId", user.getHospitalId());
            map.put("keyword", keyword);
            return upkeepService.queryPhoneByHospital(map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnResultUtil(200);
    }

}
